package com.lanou3g.hw0518.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class Main {
    public static void main(String[] args) {
        // 创建出图片的框
        BufferedImage image = new BufferedImage(300, 150,
                BufferedImage.TYPE_INT_RGB);

        // 使用画笔 进行绘制图片
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        // 绘制背景
        graphics.setBackground(Color.WHITE);
//        graphics.setColor(new Color(123,213,34));
//        graphics.fillRect(0,0,300,150);
        // 绘制文字
        graphics.setColor(new Color(0,0,0));
        // 绘制字符串时,横坐标是字符串左边
        // 纵坐标指的是基线的纵坐标
        graphics.setFont(new Font(null,3,25));
        graphics.drawString("pP",0,50);

        graphics.setColor(Color.WHITE);
        graphics.drawLine(0,0,300,150);

        File file = new File("aab.jpg");
        try {
            if (file.exists()){
                file.delete();
            }
            file.createNewFile();
            OutputStream os = new FileOutputStream("aab.jpg");
            ImageIO.write(image, "JPEG", os);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
